package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.itheima.common.TokenHolder;
import com.itheima.dao.AddressBookMapper;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import com.itheima.vo.GetDefaultsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {


    @Resource
    private AddressBookMapper addressBookMapper;

    private  static String userId =null;

    /**
     * 添加收货地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public AddressBook save(AddressBook addressBook) {

        addressBook.setId(new Snowflake().nextIdStr());
        addressBook.setUserId(userId);
        addressBook.setCreateUser(TokenHolder.getCurrentId());
        addressBook.setUpdateUser(TokenHolder.getCurrentId());

       addressBookMapper.insert(addressBook);

        return addressBookMapper.selectById(addressBook.getId());
    }

    /**
     * 获取收获地址列表
     *
     * @return
     */
    @Override
    public List<AddressBook> getAddressList() {
        userId = TokenHolder.getCurrentId();
        return addressBookMapper.getList(userId);

    }

    /**
     * 根据id获取收获地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook getAddressById(String id) {

        return addressBookMapper.getAddressBookById(id);
    }

    /**
     * 修改收获地址
     * @param addressBook
     * @return
     */
    @Override
    public Boolean updateAddress(AddressBook addressBook) {

        int update = addressBookMapper.updateAddress(addressBook);

        return update>0;
    }

    /**
     * 设置默认收货地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook updateDefault(String id) {
        addressBookMapper.updateDefault();

        int i = addressBookMapper.updateDefaultById(id);

        return addressBookMapper.selectById(id);

    }

    /**
     * 获取默认收货地址
     *
     * @return
     */
    @Override
    public AddressBook getDefaultAddress() {
        return addressBookMapper.getDefaultAddress();
    }

    /**
     * 删除收货地址
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {

        int i = addressBookMapper.deleteById(id);

        return i>0;
    }
}
